package com.tencreat.service.mall.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.model.LoginUser;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.OrderStatus;
import com.tencreat.domain.mall.MallCategory;
import com.tencreat.domain.mall.MallGoodsCollage;
import com.tencreat.domain.mall.MallGoodsKill;
import com.tencreat.domain.mall.MallGoodsNorms;
import com.tencreat.domain.mall.dto.MallGoodsCollageDTO;
import com.tencreat.domain.mall.dto.MallGoodsRecordDTO;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.domain.mall.dto.GoodsKillListAppDTO;
import com.tencreat.mapper.mall.MallCollectMapper;
import com.tencreat.mapper.mall.MallGoodsKillMapper;
import com.tencreat.service.mall.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品秒杀Service业务层处理
 *
 * @author tencreat
 * @date 2021-09-08
 */
@Service
public class MallGoodsKillServiceImpl extends ServiceImpl<MallGoodsKillMapper, MallGoodsKill> implements IMallGoodsKillService {
    @Autowired
    MallCollectMapper mallCollectMapper;
    @Autowired
    IMallAddressService mallAddressService;
    @Autowired
    IMallGoodsNormsService mallGoodsNormsService;
    @Autowired
    IMallGoodsService mallGoodsService;
    @Autowired
    IMallCategoryService categoryService;
    /**
     * 查询商品秒杀
     *
     * @param id         商品秒杀ID
     * @param loginCheck
     * @return 商品秒杀
     */
    @Override
    public HttpResult<ResGoodsKillDetailVO> selectMallGoodsKillById(ReqIdParam id, LoginUser loginCheck) {
        ResGoodsKillDetailVO res = baseMapper.selectMallGoodsKillById(id.getId());
        if (res == null) {
            return HttpResult.error("秒杀商品不存在,id:" + id.getId());
        }
        List<String> gallery = JSONObject.parseObject(res.getImage(), ArrayList.class);
        res.setGallery(gallery);
//        List<String> detail = JSONObject.parseObject(res.getImage2(), ArrayList.class);
        res.setDetail(res.getImage2());
        //快递价
        if(null != id.getAddressId()){
            BigDecimal postAgePrice = mallAddressService.getPostAgeByGood(res.getGoodsId(), id.getAddressId().intValue());
            res.setPostage(postAgePrice);
        }else {
            res.setPostage(new BigDecimal("0"));
        }
        MallGoodsNorms norms = mallGoodsNormsService.getFirstNorms(res.getGoodsId());
        res.setNormsId(norms.getId());
        res.setInventory(norms.getInventory());
        res.setIsCollect(mallCollectMapper.selectIsCollect(id.getId(), loginCheck.getAccountLogin().getId()) > 0);
        return HttpResult.ok(res);
    }

    /**
     * 查询商品秒杀列表
     *
     * @param mallGoodsKill 商品秒杀
     * @return 商品秒杀
     */
    @Override
    public List<MallGoodsKill> selectMallGoodsKillList(MallGoodsKill mallGoodsKill) {
        return baseMapper.selectList(Wrappers.query(mallGoodsKill));
    }

    /**
     * 新增商品秒杀
     *
     * @param mallGoodsKill 商品秒杀
     * @return 结果
     */
    @Override
    public int insertMallGoodsKill(MallGoodsKill mallGoodsKill) {
        mallGoodsKill.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallGoodsKill);
    }

    /**
     * 修改商品秒杀
     *
     * @param mallGoodsKill 商品秒杀
     * @return 结果
     */
    @Override
    public int updateMallGoodsKill(MallGoodsKill mallGoodsKill) {
        mallGoodsKill.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallGoodsKill);
    }

    /**
     * 批量删除商品秒杀
     *
     * @param ids 需要删除的商品秒杀ID
     * @return 结果
     */
    @Override
    public int deleteMallGoodsKillByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除商品秒杀信息
     *
     * @param id 商品秒杀ID
     * @return 结果
     */
    @Override
    public int deleteMallGoodsKillById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }


    @Override
    public Page<ResGoodsKillListAppVO> page(IPage<ResGoodsKillListAppVO> page, GoodsKillListAppDTO vo) {
        if (vo.getTwoCategoryId() == 0 && vo.getOneCategoryId() != 0) {
            List<MallCategory> list = categoryService.list(new QueryWrapper<MallCategory>().eq("pid", vo.getOneCategoryId()));
            List<Long> longs = new ArrayList<>();
            for (MallCategory mallCategory : list) {
                longs.add(mallCategory.getId());
            }
            if (longs.size() > 0) {
                vo.setCategoryIds(longs);
            } else {
                return new Page<>(vo.getPageNum(), vo.getPageSize());
            }
        }
        return baseMapper.killGoodsListPage(page, vo, LocalDateTime.now());
    }

    @Override
    public IPage<MallGoodsKillVO> getPCList(IPage<?> page, MallGoodsCollageDTO dto) {
        return baseMapper.getPCList(page, dto);
    }

    @Override
    public HttpResult<Map<String, Object>> getPCDetails(Long id) {
        return HttpResult.ok(baseMapper.getPCDetails(id));
    }

    @Override
    public MallGoodsKillStatisticsVO killStatistics() {
        LocalDateTime now = LocalDateTime.now();
        MallGoodsKillStatisticsVO vo = new MallGoodsKillStatisticsVO();
        // 秒杀成功金额
        BigDecimal killSuccess = baseMapper.killStatistics(1, null);
        vo.setKillSuccess(killSuccess);
        // 今日秒杀成功金额
        BigDecimal todayKillSuccess = baseMapper.killStatistics(1, now);
        vo.setTodayKillSuccess(todayKillSuccess);
        // 秒杀失败金额
        BigDecimal killFail = baseMapper.killStatistics(2, null);
        vo.setKillFail(killFail);
        // 今日秒杀失败金额
        BigDecimal todayKillFail = baseMapper.killStatistics(2, now);
        vo.setTodayKillFail(todayKillFail);
        return vo;
    }

    @Override
    public IPage<MallGoodsKIllRecordVo> killRecord(IPage<?> iPage, MallGoodsRecordDTO dto) {
        IPage<MallGoodsKIllRecordVo> page = baseMapper.killRecord(iPage, dto);
        page.getRecords().forEach(item ->{
            if (item.getOrderStatus() == OrderStatus.PAY.getValue()){
                item.setStatus("进行中");
            }
            else if (item.getOrderStatus() == OrderStatus.DEL_PAY.getValue()){
                item.setStatus("失败");
            }
             else {
                 item.setStatus("成功");
            }
        });
        return page;
    }

    /****
     * 上升
     * @param mallGoodsKill
     * @return
     */
    @Override
    public boolean reSortOrderReduce(MallGoodsKill mallGoodsKill) {
        MallGoodsKill one = lambdaQuery().eq(MallGoodsKill::getId, mallGoodsKill.getId()).one();
        one.setSort(one.getSort() -1);
        updateById(one);

        return true;
    }
    /****
     * 下降
     * @param mallGoodsKill
     * @return
     */
    @Override
    public boolean reSortOrderPlus(MallGoodsKill mallGoodsKill) {
        MallGoodsKill one = lambdaQuery().eq(MallGoodsKill::getId, mallGoodsKill.getId()).one();
        one.setSort(one.getSort() +1);
        updateById(one);

        return true;
    }
}
